package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Cake;
import com.entity.CakeAdmin;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.servers.CakeAdminService;

@Controller
public class AdminCakeController {
	@Autowired
	private CakeAdminService cakeAdminService;

	@ResponseBody
	@RequestMapping("/getCakeAdmins")
	public String goadminCakejsp(HttpServletRequest request) {
		Map<String, Object> map=new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", 11);
		List<Map<String, Object>> list=new ArrayList<>();
		
		String page=request.getParameter("page");
		String limit=request.getParameter("limit");
		int epage=-1;
		int elimit=-1;
		if (page!=null && limit!=null) {
			epage=Integer.parseInt(page);
			elimit=Integer.parseInt(limit);
		}
		List<CakeAdmin> allCakes = cakeAdminService.findCakeAdminLimit((epage-1)*elimit, elimit);
		for (CakeAdmin cake : allCakes) {
			Map<String, Object> tempmap=new HashMap<>();
			tempmap.put("id", cake.getCakeId());
			tempmap.put("cakename", cake.getCakeName());
			tempmap.put("cakeprice", cake.getCakePrice());
			tempmap.put("cakesales", cake.getCakeSales());
			tempmap.put("cakecontext", cake.getCakeContext());
			int id=cake.getCaketypeId();
			if (id==2 || id==3 || id==4 || id==5) {
				tempmap.put("type", "蛋糕");
			}else if (id==6) {
				tempmap.put("type", "吐司");
			}else if (id==7) {
				tempmap.put("type", "礼品");
			}
			list.add(tempmap);
		}
		List<CakeAdmin> findAllCakeAdmins = cakeAdminService.findAllCakeAdmins();
		map.put("data", list);
		map.put("count", findAllCakeAdmins.size());
		Gson gson=new Gson();
		System.out.println("-----------------------执行完毕分页查询--------------------------------");
		//System.out.println(gson.toJson(map));
		return gson.toJson(map);
	}
	@ResponseBody
	@RequestMapping("/findCakeLikeName")
	public String findCakeLikeName(String cakename) {
		System.out.println(cakename);
		Map<String, Object> map=new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", 11);
		List<Map<String, Object>> list=new ArrayList<>();
		List<CakeAdmin> allCakes = cakeAdminService.findLikeCake("%"+cakename+"%");
		for (CakeAdmin cake : allCakes) {
			Map<String, Object> tempmap=new HashMap<>();
			tempmap.put("id", cake.getCakeId());
			tempmap.put("cakename", cake.getCakeName());
			tempmap.put("cakeprice", cake.getCakePrice());
			tempmap.put("cakesales", cake.getCakeSales());
			tempmap.put("cakecontext", cake.getCakeContext());
			int id=cake.getCaketypeId();
			if (id==2 || id==3 || id==4 || id==5) {
				tempmap.put("type", "蛋糕");
			}else if (id==6) {
				tempmap.put("type", "吐司");
			}else if (id==7) {
				tempmap.put("type", "礼品");
			}
			list.add(tempmap);
		}
		map.put("data", list);
		map.put("count", allCakes.size());
		Gson gson=new Gson();
		System.out.println("-----------------------执行完毕M模糊查询--------------------------------");
		return gson.toJson(map);
	}
	@ResponseBody
	@RequestMapping("/editCake")
	public String editCake(CakeAdmin cakeAdmin,String ser) {
		System.out.println(cakeAdmin+"~~~~~~~~~~");
		System.out.println(ser.split("=")[1]+"@@@@@@@@@@@@@@@@@");
		int cakeid=Integer.parseInt(ser.split("=")[1]);
		cakeAdmin.setCakeId(cakeid);
		int caketypeId = cakeAdmin.getCaketypeId();
		if (caketypeId==0) {
			cakeAdmin.setCaketypeId(1);;
		}else if (caketypeId==1) {
			cakeAdmin.setCaketypeId(6);;
		}else if (caketypeId==2) {
			cakeAdmin.setCaketypeId(7);;
		}
		cakeAdminService.updateCakeAdminByid(cakeAdmin);
		Gson gson=new Gson();
		Map<String, Object> map=new HashMap<>();
		map.put("success", "success");
		return gson.toJson(map);
	}
	//@ResponseBody
	@RequestMapping("/addCake")
	public String addCake(CakeAdmin cakeAdmin1,String cakeName,MultipartFile file,HttpServletRequest request,Model model) {
		System.out.println(cakeAdmin1+"!!!!!!!!!!!!!!!!");
		System.out.println(cakeName+"@@@@@@@@@@@@@@@@@"+file);
		String path=request.getSession().getServletContext().getRealPath("resource");
		try {
			file.transferTo(new File(path, file.getOriginalFilename()));
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String urlimg=path+file.getOriginalFilename();
		System.out.println(urlimg+"~~~~~~~~~~~~~~~~~~~~~tianjia chenggong~~~~~~~~~~~~~~~~~~~~~");
		cakeAdmin1.setCakeimgurl(urlimg);
		int addCakeAdmin = cakeAdminService.addCakeAdmin(cakeAdmin1);
		System.out.println(addCakeAdmin+"~~~~~~~~~~~~~~~~~~~~~tianjia chenggong~~~~~~~~~~~~~~~~~~~~~");
		return "../../adminCake";
		//return gson.toJson(map);
	}
	public void setCakeAdminService(CakeAdminService cakeAdminService) {
		this.cakeAdminService = cakeAdminService;
	}
	
}
